package com.itany.cloud.controller;

import com.itany.cloud.entity.User;
import com.itany.cloud.util.ResponseResult;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@Tag(name = "用户服务功能模块",description = "用于实现用户服务功能的CURD操作")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

//    private static final String GLOBAL_URL = "http://localhost:8001/users";

    // 此时在yml中配置了provider-server.users-url,其值是http://cloud-nacos-provider
    // 那么相当于访问的是注册到Nacos中的服务cloud-nacos-provider
    // 由Nacos控制具体访问的服务
    // 例如:此时访问的具体服务是:http://127.0.0.1:9001
    @Value("${provider-server.users-url}")
    private String serverUrl;
    @Operation(summary = "根据主键查询用户信息",description = "根据前端传递过来的id值去cloud库中查询用户相关的信息")
//    @Parameter(name = "id",description = "id属性值,主键字段",required = true)
    @Parameter(name = "id",description = "id属性值,主键字段")
    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable Integer id) {
        return restTemplate.getForObject(serverUrl + "/users/{id}", ResponseResult.class, id);
    }

    @Operation(summary = "查询所有用户")
    @GetMapping
    public ResponseResult findAll() {
        return restTemplate.getForObject(serverUrl+"/users", ResponseResult.class);
    }

    @Operation(summary = "添加用户信息",description = "通过传递过来的用户名与密码添加对应用户信息,主键自增,状态与创建时间自动插入")
    @Parameters({
            @Parameter(name = "username",description = "用户名"),
            @Parameter(name = "password",description = "密码"),
    })
    @PostMapping
    public ResponseResult addUser(User user) {
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("username", user.getUsername());
        params.add("password", user.getPassword());
        return restTemplate.postForObject(serverUrl+"/users", params, ResponseResult.class);
    }

    @Operation(summary = "修改用户信息")
    @PutMapping
    public ResponseResult modifyUser(User user) {
        // 对于put、delete请求,其对应方法没有返回值
//        restTemplate.put();
//        restTemplate.delete();
        // 但是我们此时调用提供者提供的服务时,必须要有返回值
        // 因此,可以使用通用方法
        String url = new StringBuffer()
                .append(serverUrl)
                .append("/users?id={id}")
                .append("&username={username}")
                .append("&password={password}")
                .toString();
        ResponseEntity<ResponseResult> entity = restTemplate.exchange(url, HttpMethod.PUT, null, ResponseResult.class, user.getId(), user.getUsername(), user.getPassword());
        return entity.getBody();
    }

    @Hidden
    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable Integer id) {
        ResponseEntity<ResponseResult> entity = restTemplate.exchange(serverUrl + "/users/{id}", HttpMethod.DELETE, null, ResponseResult.class, id);
        return entity.getBody();
    }

}
